﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.UI;

namespace Star.Tools.Web
{
    public class WebPage
    {

        /// <summary>
        /// MyMethod()
        /// </summary>
        public static void RegisterOnlineDebug(Page page)
        {
            Uri uri1 = page.Request.UrlReferrer;
            if (((page.Request["DEBUG"] != null) && (page.Request["DEBUG"] == "true")) ||
                ((uri1 != null) && (uri1.ToString().IndexOf("DEBUG=") != -1)))
            {
                page.Error += new EventHandler(page_Error);
            }
        }
        /// <summary>
        /// 错误消息输出
        /// </summary>
        private static void page_Error(object sender, EventArgs e)
        {
            Page page1 = (Page)sender;
            Exception exception1 = page1.Server.GetLastError();
            StringBuilder builder1 = new StringBuilder();
            builder1.Append("<div style=\"font-size:10pt;font-family:verdana;line-height:150%;\">");
            builder1.AppendFormat("<strong>\u9519\u8bef\u6d88\u606f\uff1a</strong>{0} \n", exception1.Message);
            builder1.AppendFormat("<strong>\u5bfc\u81f4\u9519\u8bef\u7684\u5e94\u7528\u7a0b\u5e8f\u6216\u5bf9\u8c61\u7684\u540d\u79f0</strong>\uff1a{0} \n", exception1.Source);
            builder1.AppendFormat("<div style=\"background-color:yellow;color:red;padding:12px;\"><strong>\u5806\u6808\u5185\u5bb9</strong>\uff1a{0} </div>\n", exception1.StackTrace);
            builder1.AppendFormat("<strong>\u5f15\u53d1\u5f02\u5e38\u7684\u65b9\u6cd5</strong>\uff1a{0} \n", exception1.TargetSite);
            builder1.AppendFormat("<strong>\u9519\u8bef\u9875\u9762</strong>\uff1a{0} \n", page1.Request.RawUrl);
            builder1.Append("</div>");
            page1.Server.ClearError();
            page1.Response.Write(builder1.ToString().Replace("\n", "<br/>"));
        }
    }
}
